package cn.edu.ctbu.datajpa01.webapi;

import cn.edu.ctbu.datajpa01.core.PageUtils;
import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {
    @Autowired
    private StudentService studentService;

@GetMapping("/list")
    public List<Student> getAll(){
        List<Student> students=studentService.findAll();

        return students;
    }

    /**
     * 分页都区全部内容
     */
//    @GetMapping("/getbypage")
//    public PageUtils getByPage(@RequestParam(value="page",defaultValue = "0") Integer page,
//                               @RequestParam(value="size",defaultValue = "10")Integer size){
//        Pageable pageable= PageRequest.of(page,size, Sort.by("id").descending());
//        Page<Student> studentPage=studentService.findAll(pageable);
//
//      PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
//        return pageUtils;
//    }


    /**
     * 分页过滤查询
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "") String name,
                               @RequestParam(value = "no",defaultValue = "") String no)
    {
//        Pageable pageable= PageRequest.of(page,size,Sort.by("id").descending());
        Page<Student> studentPage;
//                =studentService.findAll(pageable);
        //参数为空，正常查询
        if (StringUtils.isEmpty(name)&&StringUtils.isEmpty(no)){
            Pageable pageable= PageRequest.of(page,size,Sort.by("id").descending());
            studentPage=studentService.findAll(pageable);

        }
        //匹配学号查询
        else  if (StringUtils.hasText(no))  {
        Student student=new Student();
        student.setNo(no);
        Pageable pageable=  PageRequest.of(0,3, Sort.by("no").descending());
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withMatcher("no",ExampleMatcher.GenericPropertyMatchers.contains());//全部模糊查询，即%{no)%
            // 通过Example构建查询条件  特别注意这个Example对象是spring data中的依赖，别引成了hibernate的
            Example<Student> example=Example.of(student,matcher);

        studentPage=studentService.findAll(example,pageable);

    }
        //匹配姓名查询
        else  {
            Student student=new Student();
            student.setName(name);
            Pageable pageable=  PageRequest.of(0,3, Sort.by("id").ascending());
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example=Example.of(student,matcher);
            studentPage=studentService.findAll(example,pageable);
        }


        PageUtils pageUtils=new PageUtils(studentPage.getContent(), studentPage.getTotalElements());
        return pageUtils;
    }

    /**
     * 新增学生的方法，/webapi/student/insert
     * @param student
     * @return
     */
@PostMapping("/insert")
    public  Student insert(Student student){

       Student student1 = studentService.insert(student);
       return student1;

    }




    /**
     * 更新学生的方法，/webapi/student/insert
     * @param student
     * @return
     */
    @PutMapping("/update")
    public  Student update(Student student){
        //读取旧数据
        Student oldstudent=studentService.getById(student.getId());
        if(StringUtils.isEmpty(student.getPassword())){
            //没有新密码，那么就是旧密码
            student.setPassword(oldstudent.getPassword());
        }
        student = studentService.update(student);
        return student;

    }


    /**
     * 删除学生的方法，/webapi/student/insert
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
//    通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中：URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。
    public  void delete(@PathVariable  Integer id){

       studentService.delete(id);

    }
    /**
     * 读取的url:/webapi/student/get/id
     * @return
     */

    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id){
       Student student=studentService.getById(id);
       student.setPassword("");

        return student;
    }


}



